package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblFlowMySql;
import com.huabo.system.mysql.entity.TblSystemModelFlowMySql;
import com.huabo.system.mysql.entity.TblSystemModuleMySql;
import com.huabo.system.mysql.mapper.TblFlowMySqlMapper;
import com.huabo.system.mysql.mapper.TblSystemModelFlowMySqlMapper;
import com.huabo.system.mysql.mapper.TblSystemModuleMySqlMapper;
import com.huabo.system.oracle.entity.TblFlow;
import com.huabo.system.oracle.entity.TblSystemModelFlow;
import com.huabo.system.oracle.entity.TblSystemModule;
import com.huabo.system.oracle.mapper.TblFlowMapper;
import com.huabo.system.oracle.mapper.TblSystemModelFlowMapper;
import com.huabo.system.oracle.mapper.TblSystemModuleMapper;
import com.huabo.system.service.TblSystemModuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class TblSystemModuleServiceImpl implements TblSystemModuleService {

    @Resource
    private TblSystemModuleMapper systemModuleMapper;

    @Resource
    private TblSystemModelFlowMapper tblSystemModelFlowMapper;

    @Resource
    private TblFlowMapper tblFlowMapper;

    @Resource
    private TblSystemModuleMySqlMapper systemModuleMySqlMapper;

    @Resource
    private TblSystemModelFlowMySqlMapper tblSystemModelFlowMySqlMapper;

    @Resource
    private TblFlowMySqlMapper tblFlowMySqlMapper;


    @Override
    public Map<String, Object> selectPageInfoByOrgId(Integer pageNumber, Integer pageSize, String token, String staffId, TblSystemModule module) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            PageInfo<TblSystemModule> pageInfo = new PageInfo<TblSystemModule>();
            pageInfo.setPageSize(pageSize);
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setTlist(systemModuleMapper.selectPageInfoByOrgId(pageInfo, orgid, module));
            pageInfo.setTotalRecord(systemModuleMapper.selectCountByPageInfo(orgid, module));
            for (TblSystemModule mo : pageInfo.getTlist()) {
                List<TblSystemModelFlow> flow = systemModuleMapper.findFlowByModule(mo.getModelId());
                List<TblFlow> flowList = new ArrayList<>();
                for (TblSystemModelFlow fl : flow) {
                    TblFlow tblFlow = new TblFlow();
                    tblFlow = tblFlowMapper.findByflowId(fl.getFlowId());
                    flowList.add(tblFlow);
                }
                mo.setFlowList(flowList);
            }

            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
            resultMap.put("data", pageInfo);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> selectMySqlPageInfoByOrgId(Integer pageNumber, Integer pageSize, String token, String staffId, TblSystemModuleMySql module) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();
            PageInfo<TblSystemModuleMySql> pageInfo = new PageInfo<TblSystemModuleMySql>();
            pageInfo.setPageSize(pageSize);
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setTlist(systemModuleMySqlMapper.selectPageInfoByOrgId(pageInfo, orgid, module));
            pageInfo.setTotalRecord(systemModuleMySqlMapper.selectCountByPageInfo(orgid, module));
            for (TblSystemModuleMySql mo : pageInfo.getTlist()) {
                List<TblSystemModelFlowMySql> flow = systemModuleMySqlMapper.findFlowByModule(mo.getModelId());
                List<TblFlowMySql> flowList = new ArrayList<>();
                for (TblSystemModelFlowMySql fl : flow) {
                    TblFlowMySql tblFlow = new TblFlowMySql();
                    tblFlow = tblFlowMySqlMapper.findByflowId(fl.getFlowId());
                    flowList.add(tblFlow);
                }
                mo.setFlowList(flowList);
            }

            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
            resultMap.put("data", pageInfo);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Integer checkReplay(TblSystemModule module, int type, String token, String staffId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Integer count = 0;
            try {
                TblStaffUtil user = DealUserToken.parseUserToken(token);
                module.setCreateTime(new Date());
                module.setCreatePerson(user.getStaffid());
                module.setModelStatus(0);
                module.setModelOrg(user.getCurrentOrg().getOrgid());
                module.setModifyTime(new Date());
                module.setModifyPerson(user.getStaffid());
                String sql;
                BigDecimal orgid = module.getModelOrg();
                Integer modelId = module.getModelId();
                String modelName = module.getModelName();
                String modelNo = module.getModelNo();
                if (type == 1) {
                    //修改时验证
                    count = systemModuleMapper.selectUpdateCount(orgid, modelId, modelName);
                } else {
                    //新增时候验证
                    count = systemModuleMapper.selectInsertCount(orgid, modelNo, modelName);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return count;
        } else {
            Integer count = 0;
            try {
                TblStaffUtil user = DealUserToken.parseUserToken(token);
                module.setCreateTime(new Date());
                module.setCreatePerson(user.getStaffid());
                module.setModelStatus(0);
                module.setModelOrg(user.getCurrentOrg().getOrgid());
                module.setModifyTime(new Date());
                module.setModifyPerson(user.getStaffid());
                String sql;
                BigDecimal orgid = module.getModelOrg();
                Integer modelId = module.getModelId();
                String modelName = module.getModelName();
                String modelNo = module.getModelNo();
                if (type == 1) {
                    //修改时验证
                    count = systemModuleMySqlMapper.selectUpdateCount(orgid, modelId, modelName);
                } else {
                    //新增时候验证
                    count = systemModuleMySqlMapper.selectInsertCount(orgid, modelNo, modelName);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return count;
        }
    }

    @Override
    public Integer checkMySqlReplay(TblSystemModuleMySql module, int type, String token, String staffId) {
        return null;
    }

    @Override
    public Map<String, Object> saveEntity(String token, String staffId, TblSystemModule module, Integer[] flowid,
                                          Integer[] flowOrderNo) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil user = DealUserToken.parseUserToken(token);
            module.setCreateTime(new Date());
            module.setCreatePerson(user.getStaffid());
            module.setModelStatus(0);
            module.setModelOrg(user.getCurrentOrg().getOrgid());
            systemModuleMapper.insertSystemModule(module);
            TblSystemModelFlow modelFlow = null;
            List<TblSystemModelFlow> list = new ArrayList<TblSystemModelFlow>(0);
            if (flowid != null) {
                for (int i = 0; i < flowid.length; i++) {
                    modelFlow = new TblSystemModelFlow();
                    modelFlow.setFlowId(flowid[i]);
                    modelFlow.setModelNo(flowOrderNo[i]);
                    list.add(modelFlow);
                }
            }
            for (TblSystemModelFlow flow : list) {
                flow.setModelId(module.getModelId());
                tblSystemModelFlowMapper.insertFlow(flow);
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
            resultMap.put("data", "0");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> saveMySqlEntity(String token, String staffId, TblSystemModuleMySql module, Integer[] flowid, Integer[] flowOrderNo) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil user = DealUserToken.parseUserToken(token);
            module.setCreateTime(new Date());
            module.setCreatePerson(user.getStaffid());
            module.setModelStatus(0);
            module.setModelOrg(user.getCurrentOrg().getOrgid());
            systemModuleMySqlMapper.insertSystemModule(module);
            TblSystemModelFlowMySql modelFlow = null;
            List<TblSystemModelFlowMySql> list = new ArrayList<TblSystemModelFlowMySql>(0);
            if (flowid != null) {
                for (int i = 0; i < flowid.length; i++) {
                    modelFlow = new TblSystemModelFlowMySql();
                    modelFlow.setFlowId(flowid[i]);
                    modelFlow.setModelNo(flowOrderNo[i]);
                    list.add(modelFlow);
                }
            }
            for (TblSystemModelFlowMySql flow : list) {
                flow.setModelId(module.getModelId());
                tblSystemModelFlowMySqlMapper.insertFlow(flow);
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
            resultMap.put("data", "0");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public TblSystemModule findAllInfoById(Integer moduleId) {
        return systemModuleMapper.selectBymoduleId(moduleId);
    }

    @Override
    public TblSystemModuleMySql findAllMySqlInfoById(Integer moduleId) {
        return systemModuleMySqlMapper.selectBymoduleId(moduleId);
    }

    @Override
    public Map<String, Object> modifyEntity(String token, String staffId, TblSystemModule module, Integer[] flowid, Integer[] flowOrderNo) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil user = DealUserToken.parseUserToken(token);
            module.setModelOrg(user.getCurrentOrg().getOrgid());
            module.setModifyTime(new Date());
            module.setModifyPerson(user.getStaffid());
            TblSystemModule oldModule = this.systemModuleMapper.selectBymoduleId(module.getModelId());
            this.systemModuleMapper.deleteByModulId(module.getModelId());
            oldModule.setModelName(module.getModelName());
            oldModule.setModifyPerson(module.getModifyPerson());
            oldModule.setModifyTime(module.getModifyTime());
            oldModule.setModelUrl(module.getModelUrl());
            oldModule.setModelType(module.getModelType());
            oldModule.setModelNo(module.getModelNo());
            this.systemModuleMapper.updateByModule(oldModule);
            TblSystemModelFlow modelFlow = null;
            List<TblSystemModelFlow> list = new ArrayList<TblSystemModelFlow>(0);
            if (flowid != null) {
                for (int i = 0; i < flowid.length; ++i) {
                    modelFlow = new TblSystemModelFlow();
                    modelFlow.setFlowId(flowid[i]);
                    //modelFlow.setModelNo(flowOrderNo[i]);
                    list.add(modelFlow);
                }
            }
            for (TblSystemModelFlow flow : list) {
                flow.setModelId(module.getModelId());
                tblSystemModelFlowMapper.insertFlow(flow);
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
            resultMap.put("data", "0");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> modifyMySqlEntity(String token, String staffId, TblSystemModuleMySql module, Integer[] flowid, Integer[] flowOrderNo) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil user = DealUserToken.parseUserToken(token);
            module.setModelOrg(user.getCurrentOrg().getOrgid());
            module.setModifyTime(new Date());
            module.setModifyPerson(user.getStaffid());
            TblSystemModuleMySql oldModule = this.systemModuleMySqlMapper.selectBymoduleId(module.getModelId());
            this.systemModuleMySqlMapper.deleteByModulId(module.getModelId());
            oldModule.setModelName(module.getModelName());
            oldModule.setModifyPerson(module.getModifyPerson());
            oldModule.setModifyTime(module.getModifyTime());
            oldModule.setModelUrl(module.getModelUrl());
            oldModule.setModelType(module.getModelType());
            oldModule.setModelNo(module.getModelNo());
            this.systemModuleMySqlMapper.updateByModule(oldModule);
            TblSystemModelFlowMySql modelFlow = null;
            List<TblSystemModelFlowMySql> list = new ArrayList<TblSystemModelFlowMySql>(0);
            if (flowid != null) {
                for (int i = 0; i < flowid.length; ++i) {
                    modelFlow = new TblSystemModelFlowMySql();
                    modelFlow.setFlowId(flowid[i]);
                    //modelFlow.setModelNo(flowOrderNo[i]);
                    list.add(modelFlow);
                }
            }
            for (TblSystemModelFlowMySql flow : list) {
                flow.setModelId(module.getModelId());
                tblSystemModelFlowMySqlMapper.insertFlow(flow);
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
            resultMap.put("data", "0");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public void modifyFlowModifyStatus(Integer moduleStatus, Integer moduleId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            systemModuleMapper.updateByModuleId(moduleStatus, moduleId);
        } else {
            systemModuleMySqlMapper.updateByModuleId(moduleStatus, moduleId);
        }
    }

    @Override
    public String findModelTypeByModuleId(Integer moduleId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return systemModuleMapper.selectByModuleId(moduleId);
        } else {
            return systemModuleMySqlMapper.selectByModuleId(moduleId);
        }
    }

    @Override
    public Integer selectOrgManageRight(String id, String rightId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return systemModuleMapper.selectOrgManageRightId(id, rightId);
        } else {
            return systemModuleMySqlMapper.selectOrgManageRightId(id, rightId);
        }
    }

    @Override
    public void saveOrgRightRelation(String id, String rightId, int type) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            if (type == 1) {
                this.systemModuleMapper.insertModule(id, rightId);
            } else {
                this.systemModuleMySqlMapper.insertModuleByrightId(id, rightId);
            }
        } else {
            if (type == 1) {
                this.systemModuleMapper.insertModule(id, rightId);
            } else {
                this.systemModuleMySqlMapper.insertModuleByrightId(id, rightId);
            }
        }
    }

    @Override
    public void dealModuleFlowOrganizationRealtion(Integer moduleId, String orgId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            this.systemModuleMapper.deleteByIdAndModuleId(moduleId, orgId);
            this.systemModuleMapper.insertByIdAndModuleId(moduleId, orgId);
        } else {
            this.systemModuleMySqlMapper.deleteByIdAndModuleId(moduleId, orgId);
            this.systemModuleMySqlMapper.insertByIdAndModuleId(moduleId, orgId);
        }
    }

    @Override
    public void removeStaffModuleRelation(Integer moduleId, String orgId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            this.systemModuleMapper.deleteBymoduleIdAndOrgId(moduleId, orgId);
        } else {
            this.systemModuleMySqlMapper.deleteBymoduleIdAndOrgId(moduleId, orgId);
        }
    }

    @Override
    public void removeModelFlowRelation(String[] flowIds, Integer modelId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            for (String s : flowIds) {
                this.systemModuleMapper.removeModelFlowRelation(s, modelId);
            }
        } else {
            for (String s : flowIds) {
                this.systemModuleMySqlMapper.removeModelFlowRelation(s, modelId);
            }
        }
    }
}
